<!--
 * 全宗实体 单体编辑组件
 * :
 * @version: 1.0
 * @author: lakeside hubin_yuan.410@hotmail.com
 * @date: 20120-07-14
 * @LastEditors:
 * @LastEditTime:
 -->
<template>
  <el-col>
    <el-row class="p-1">
      <dossierFondsEntityForm ref="dossierFondsEntityForm" v-model="form"></dossierFondsEntityForm>
    </el-row>
    <el-row justify="center" type="flex">
      <el-button v-if="!form.editable" type="primary" @click="formMode(true,'编辑',true)">编辑</el-button>
      <el-button v-if="form.editable" @click="formMode(true,'详情',false)">取 消</el-button>
      <el-button v-if="form.editable" type="primary" @click="formSubmit">保存</el-button>
    </el-row>
  </el-col>

</template>

<script>
import *  as dossierFondsEntityApi from '../apis/fondsEntity'
import {form_mixin} from "@/mixins"
import dossierFondsEntityForm from "./components/form"

export default {
  name: "dossierFondsEntityEntry",
  props: {
    id: {type: String, default: '0'},
  },
  components: dossierFondsEntityForm,
  mixins: [form_mixin],
  data() {
    return {}
  },
  mounted() {
    this.refreshLoad()
  },
  methods: {
    /*刷新数据*/
    async refreshLoad() {
      if (this.id) {
        dossierFondsEntityApi.find({idArr: this.id}).then(response => {
          if (response.code === 200) {
            this.form.entry = response.data;
          }
        }).catch(() => {
        })
      }
    },
    /**表单保持按钮提交*/
    async formSubmit() {
      await this.$refs.dossierFondsEntityForm.validate((valid) => {
        if (valid) {
          this.saveEntity(this.$refs.dossierFondsEntityForm.form.entry)
          this.form.visible = false
        } else {
          this.$message.error('数据校验失败,请修改后再次提交！')
        }
      }).catch(() => {

      })
    },
    /*调保存接口*/
    async saveEntity(entry) {
      dossierFondsEntityApi.save(entry).then(response => {
        if (response.code === 200) {
          this.$message.success('保存成功！')
          this.form.visible = false
          this.refreshLoad();
        } else {
          this.$message({
            type: 'error',
            dangerouslyUseHTMLString: true,
            message: `操作错误！{{status}}：<br> {{message}}`.render({
              status: response.code || '',
              message: (response.message || '').replace(/\r\n/g, "<br>")
            })
          })
        }
      })
    },
  }
}
</script>

<style lang="scss" scoped>

</style>
